package cn.com.youyuan.SDK.models;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import cn.com.youyuan.SDK.common.RmsInputStream;
import cn.com.youyuan.SDK.common.RmsOutputStream;

public class UserData extends BaseDataElement {

	private String m_uid;
	private String fromId;
	private String sessionid;
	
	public void setUId(String uid) {
		this.m_uid = uid;
	}
	
	public String getUId() {
		return m_uid;
	}

	public String getFromId() {
		return fromId;
	}

	public void setFromId(String fromId) {
		this.fromId = fromId;
	}

	public String getSessionId() {
		return sessionid;
	}

	public void setSessionId(String sessionid) {
		this.sessionid = sessionid;
	}

	public byte[] externalize() throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		RmsOutputStream out = new RmsOutputStream(baos);

		out.writeUTF(fromId);
		out.writeUTF(sessionid);

		return baos.toByteArray();
	}

	public static UserData internalize(byte[] rawData)
	{
		try
		{
			ByteArrayInputStream bais = new ByteArrayInputStream(rawData);
			RmsInputStream in = new RmsInputStream(bais);
			UserData cookie = new UserData();
			cookie.setFromId(in.readUTF());
			cookie.setSessionId(in.readUTF());

			return cookie;
		}
		catch (IOException e)
		{
			return null;
		}
	}

	public String getId() {
		// TODO Auto-generated method stub
		return "userid";
	}
}
